package com.campus.wall;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.CountDownTimer;
import android.widget.TextView;

import com.campus.wall.base.BaseMvpActivity;
import com.campus.wall.contract.WelcomContract;
import com.campus.wall.presenter.WelComPresenter;

import butterknife.BindView;
import butterknife.OnClick;

public class WelcomActivity  extends BaseMvpActivity<WelComPresenter> implements WelcomContract.View {

    private CountDownTimer timer;
    @BindView(R.id.time)
    TextView timecontent;

    @Override
    public int getLayoutId() {
        return R.layout.layout_welcom;
    }

    @Override
    public void initView() {
        initCountDown();
    }
    // 倒计时逻辑
    private void initCountDown() {
        // 判断当前Activity是否isFinishing()，
        // 避免在finish，所有对象都为null的状态下执行CountDown造成内存泄漏
        if (!isFinishing()) {
            timer = new CountDownTimer(1000 * 5, 1000) {
                @SuppressLint("SetTextI18n")
                @Override
                public void onTick(long millisUntilFinished) {
                    // TODO: 耗时操作，如异步登录
                    // ......
                    int time = (int) millisUntilFinished;
                    timecontent.setText(time / 1000 +"S");

                }
                @Override
                public void onFinish() {
                    checkToJump();
                }
            }.start();
        }
    }

    private void checkToJump() {
        //  TODO：首次安装判断
        // 如果是首次安装打开，则跳至引导页；否则跳至主界面
        // 这里先不放引导页，直接跳到主界面
        startActivity(new Intent(this, LoginActivity.class));
        // 回收内存
        destoryTimer();
        finish();
    }

    public void destoryTimer() {
        // 避免内存泄漏
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    @OnClick(R.id.btn_into)
    void btn_into(){
        checkToJump();
    }
    @Override
    public void onSuccess() {

    }
}
